/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pracamagisterska;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

/**
 *
 * @author Adrian
 */
public class Chart extends ModalityInternalFrame {

    ChartPanel chartPanel;
    JPanel panel;

    /**
     * Creates new form Chart
     */
    public Chart(JComponent parent, PieDataset dataset, String title, String text) {
        initComponents();
        JFreeChart chart = createPieChart(dataset, title);
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 470));
        this.setResizable(true);
        this.setClosable(true);
        setLayout(new BorderLayout(0, 5));
        add(chartPanel, BorderLayout.CENTER);
        chartPanel.setMouseWheelEnabled(true);
        panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(createDataTable(dataset, text));
        panel2.add(createPrintChart());
        panel2.add(createPrintTable());
        panel2.add(createClose());
        this.add(panel, BorderLayout.SOUTH);
        this.add(panel2, BorderLayout.NORTH);
        this.setMinimumSize(new Dimension(500, 500));
    }

    public Chart(JComponent parent, CategoryDataset dataset, String title, String text) {
        initComponents();
        JFreeChart chart = createCategoryChart(dataset, title);
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        this.setResizable(true);
        this.setClosable(true);
        //setContentPane(chartPanel);
        setLayout(new BorderLayout(0, 5));
        add(chartPanel, BorderLayout.CENTER);
        chartPanel.setMouseWheelEnabled(true);
        panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(createDataTable2(dataset, text));
        panel2.add(createPrintChart());
        panel2.add(createPrintTable());
        panel2.add(createClose());
        this.add(panel, BorderLayout.SOUTH);
        this.add(panel2, BorderLayout.NORTH);
        this.setMinimumSize(new Dimension(500, 500));

    }

    private JFreeChart createCategoryChart(CategoryDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createBarChart(
                title, // chart title
                "", // domain axis label
                "", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                true, // include legend
                true, // tooltips?
                false);                 // URLs?
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI * 1 / 2));
        return chart;
    }

    private JFreeChart createPieChart(PieDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createPieChart3D(title,
                dataset,
                true,
                true,
                false);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
    }

    private JButton createPrintChart() {
        final JButton print = new JButton(new AbstractAction("Drukuj wykres") {

            @Override
            public void actionPerformed(ActionEvent e) {
                PrinterJob pj = PrinterJob.getPrinterJob();
                pj.setJobName(" Print Component ");
                pj.setPrintable(new ComponentPrintable(chartPanel));
                if (!pj.printDialog()) {
                    return;
                }
                try {
                    pj.print();
                } catch (PrinterException ex) {
                    System.out.println(ex);
                }
            }
        });
        print.setPreferredSize(new Dimension(105, 99));
        print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_printer.png")));
        print.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        print.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        return print;
    }

    private JButton createPrintTable() {
        final JButton print = new JButton(new AbstractAction("Drukuj dane") {

            @Override
            public void actionPerformed(ActionEvent e) {
                PrinterJob pj = PrinterJob.getPrinterJob();
                pj.setJobName(" Print Component ");
                pj.setPrintable(new ComponentPrintable(panel));
                if (!pj.printDialog()) {
                    return;
                }
                try {
                    pj.print();
                } catch (PrinterException ex) {
                    System.out.println(ex);
                }
            }
        });
        print.setPreferredSize(new Dimension(105, 99));
        print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_printer.png")));
        print.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        print.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        return print;
    }

    private JButton createClose() {
        final JButton close = new JButton(new AbstractAction("Zamknij") {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        close.setPreferredSize(new Dimension(105, 99));
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/childish_cross72.png")));
        close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        return close;
    }

    private JTable createDataTable(PieDataset dataset, String text) {
        final JTable table = new JTable(0, 10);
        table.setEnabled(false);
        int j = 0;
        int k = 0;
        addRow(table);
        for (int i = 0; i < 10; i++) {
            table.setValueAt(text, 0, i);
            table.setValueAt("Ilość", 0, ++i);
        }
        for (int i = 0; i < dataset.getItemCount(); i++) {
            if (i % 5.0 == 0) {
                j = 0;
                addRow(table);
                k++;
            } else {
                j++;
            }
            table.setValueAt(dataset.getKey(i), k, j);
            j++;
            table.setValueAt(dataset.getValue(i).intValue(), k, j);
        }
        return table;
    }

    private JTable createDataTable2(CategoryDataset dataset, String text) {
        final JTable table = new JTable(0, 10);
        table.setEnabled(false);
        int j = 0;
        int k = 0;
        addRow(table);
        for (int i = 0; i < 10; i++) {
            table.setValueAt(text, 0, i);
            table.setValueAt("Ilość", 0, ++i);
        }
        for (int i = 0; i < dataset.getColumnCount(); i++) {
            if (i % 5.0 == 0) {
                j = 0;
                addRow(table);
                k++;
            } else {
                j++;
            }
            table.setValueAt(dataset.getColumnKey(i), k, j);
            j++;
            table.setValueAt(dataset.getValue(0, i).intValue(), k, j);
        }
        return table;
    }

    private void addRow(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(new Object[]{null, null, null, null, null, null, null, null, null, null});
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 718, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
